Shader "Converted/VolumeFog"
{
    Properties
    {
        _MainTex ("_MainTex", 2D) = "white" {}
        _FirstTex ("iChannel0", 2D) = "white" {}
        _SecondTex ("iChannel1", 2D) = "white" {}
        _ThirdTex ("iChannel2", 2D) = "white" {}
        _FourthTex ("iChannel3", 2D) = "white" {}

        _Noise3DTex ("3D Texture", 3D) = "" {}
        _NoiseScale("_NoiseScale",Range(0.5,0.1)) = 0.5
        _NoiseScale2("_NoiseScale2",Range(0.5,0.1)) = 0.5
    
        _Density("_Density",Range(10,1000)) = 25
        _Near("Near",range(0,100)) = 10
        _FogDir("_FogDir",Vector)  = (0.01,0.01,0,0)
 
        _FogPower("_FogPower",Range(0.001,0.1)) = 1
        _FogColor("_FogColor",Color) = (1,1,1,1)
   
        _AO("_AO",Range(0,0.5)) = 0.1
 
        [KeywordEnum(ONE, TOW,THREE)] type("TYPE", Float) = 0
    }
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag


            #pragma multi_compile    TYPE_ONE TYPE_TOW TYPE_THREE
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

          
            float _Near;
            float _NoiseScale;
            float _NoiseScale2;
            float _Density;
            sampler2D _MainTex;
      

            // Global access to uv data
            static v2f vertex_output;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv =  v.uv;
                return o;
            }

 
 
#define time _Time.y
          
          #define STPE  20

            sampler3D _Noise3DTex;
            float4 _FogColor;
 
            float _FogPower;
            float4 _FogDir;
       
            float _FogMax;
            float _AO;
             #include "depthHelper.hlsl"
            float4 frag (v2f i) : SV_Target
            {
                vertex_output = i;
                float4 fragColor = 0;
 
                
                
 
                float3 rightdir = normalize(_WorldSpaceLightPos0.xyz);
 
                
                
 
                
                 float3 ro  ;
           
                float3 rd  ;
                GetViewDirectNoNormalize(i,ro,rd);

                float3 wpos = ro+rd;


                float dis = length(rd);
                rd = normalize(rd);
 

                
               
                float3 col = tex2D(_MainTex,i.uv);
                
          
 

                
                
                float fogFactor =  0;

                float delta = _Density/( (float)STPE);
                float dis2 = _Near;
                float density = 0;
                _FogDir *=_Time.y;
                float3 offset = half3(_FogDir.x,0,_FogDir.y)  ;

       
                float3 offset2 = half3(_FogDir.z,0,_FogDir.w)  ;
              
                float3 p = ro+rd *3  ;

                float noise = tex3D(_Noise3DTex,p*_NoiseScale + offset ).r;

                float noise2 = tex3D(_Noise3DTex,p*_NoiseScale2 + offset2 + float3(0.3,0.437,0.436) ).r;

 
                float noise3 = (noise+noise2)   ;
                float t3 =  (dis - _Near)/_Density ;
            
                fogFactor = noise3 *  t3    ;

                
                
                
                float3 fogCol = _FogColor.rgb ;
        
               
 
                fogFactor = clamp(fogFactor,0.0,_FogColor.a)   ;
                
                fogCol *= (1-noise3*_AO);  
                col = lerp(col,fogCol.rgb,fogFactor);
 
                fragColor = float4(col, 1.);
                 
                return fragColor;
            }
            ENDCG
        }
    }
}
